//重置按钮的实现
function chongzhi() {
	event.target.parentNode.parentNode.firstElementChild.firstElementChild.value = '';
	$('#hyxxquan .xyxx').each(function() {
		this.innerHTML = '';
	})

}
//订单数量以及订单总金额的变化
function dingdan() {
	let now = new Date();
	let sum = 0;
	let money = 0;
	//	let now=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
	$('#spxxdd tr').each(function() {
		let i = this.getElementsByTagName('td')[2];
		let j = this.getElementsByTagName('td')[3];
		sum += parseInt(i.innerHTML);
		money += parseInt(i.innerHTML) * parseInt(j.innerHTML);
	})
	let tds = $('#dingdanquan').get(0).getElementsByClassName('dingdan');
	tds[0].innerHTML = '111';
	tds[1].innerHTML = now.toLocaleString();
	tds[2].innerHTML = sum;
	tds[3].innerHTML = money;
}
//删除商品订单
function shanchu() {
	event.target.parentNode.parentNode.remove();
	dingdan();
}

//点击结算
function jiesuan(){
	$("#tjdd").modal("hide");
	let tds = $('#tjdd .xfddtj');
	let tbs = $('#dingdanquan .dingdan');
	let money = $('#hyxxquan .xyxx').get(3).innerHTML;
	if(parseInt(tds[5].innerHTML)>=0||parseInt(money)-parseInt(tds[2].innerHTML)>=0){
		toastr.success('消费成功');
	}else{
		toastr.error('消费失败');
	}
}
//点击提交订单按钮
function tijiao(){
	$("#tjdd").modal("show");
	let tds = $('#tjdd .xfddtj');
	let tbs = $('#dingdanquan .dingdan');
	let money = $('#hyxxquan .xyxx').get(3).innerHTML;
	if(money == '') {
		toastr.info('您正处于游客模式');
		tds[0].innerHTML = tbs[0].innerHTML;
		tds[1].innerHTML = tbs[2].innerHTML;
		tds[2].innerHTML = tbs[3].innerHTML;
		tds[3].innerHTML="";
		tds[4].disabled="";
		tds[5].innerHTML="";
		tds[4].onblur=function(){
			tds[5].innerHTML=parseInt(tds[4].value)-parseInt(tds[2].innerHTML);
		}
	} else {
		tds[0].innerHTML = tbs[0].innerHTML;
		tds[1].innerHTML = tbs[2].innerHTML;
		tds[2].innerHTML = tbs[3].innerHTML;
		tds[3].innerHTML = money;
		tds[4].value="";
		tds[4].disabled="disabled";
		tds[5].innerHTML=0-parseInt(tds[2].innerHTML);
		toastr.success('您正处于刷卡模式');
	}
}
$(function() {
	//从商品信息中往消费信息中添加商品数据
	$('#spxx tr').each(function() {
		$(this).click(function() {
			let tbs = this.getElementsByTagName('td');
			let pd = true;
			$('#spxxdd tr').each(function() {
				let tid = this.getElementsByTagName('td')[0].innerHTML;
				if(tid == tbs[0].innerHTML) {
					this.getElementsByTagName('td')[2].innerHTML = parseInt(this.getElementsByTagName('td')[2].innerHTML) + 1;
					pd = false;
				}
			})
			if(pd) {
				let tr = '<tr><td>' + tbs[0].innerHTML + '</td><td>' + tbs[1].innerHTML + '</td><td>' + 1 + '</td><td>' + tbs[5].innerHTML + '</td><td><input type="button" class="btn btn-danger btn-sm" onclick="shanchu()" value="删除" /></td></tr>';
				$('#spxxdd').append(tr);
			}
			dingdan();
			$('#hyxq').modal('hide');
		})
	})
	//会员详情中点击数据添加到会员信息中
	$('#hyxq table tr').each(function() {
		$(this).dblclick(function() {
			toastr.success('读卡成功');
			let tbs = this.getElementsByTagName('td');
			let tds = $('#hyxxquan .xyxx');
			tds[0].innerHTML = tbs[2].innerHTML;
			tds[1].innerHTML = tbs[1].innerHTML;
			tds[2].innerHTML = tbs[0].innerHTML;
			tds[3].innerHTML = tbs[4].innerHTML;
			tds[4].innerHTML = tbs[3].innerHTML;
			tds[5].innerHTML = tbs[5].innerHTML;
			$('#hyxq').modal('hide')
		})
	})
})